<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1num }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            @click="options2Fn(index, item.type)"
            :class="{ active: index == options1num }"
            v-for="(item, index) in options2"
            :key="index"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <p style="text-align: center; margin-top: 30px; color: red">
        {{ isTextbotBoom ? "已经加载完毕...." : "正在加载中..." }}
      </p>
    </div>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { productsAPI } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      // >精品推荐 2-> 热门兑换 0->全部
      did: 0,
      //( 1->实体商品 2->虚拟商品  0->全部 )
      type: 0,
      // 大于多少积分，最少是0
      min: 0,
      max: 0,
      keyword: "",
      goodsList: [],
      goodsListShow: [],
      page: 1,
      size: 8,
      isloading: false,
      //  控制加载完毕之后的提示信息显示隐藏
      isTextbotBoom: false,
      // 排序的数组项
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1num: 0,
      // 分类数组项
      options2: [
        { type: 0, txt: "全部", isActive: true },
        { type: 1, txt: "实体商品", isActive: false },
        { type: 2, txt: "虚拟商品", isActive: false },
      ],
    };
  },
  components: {
    Crumb,
    List,
  },
  created() {
    console.log("keyword:", this.$route.query.keyword);
    this.keyword = this.$route.query.keyword;
    this.getGoods();
  },

  mounted() {
    // 监听滚动事件
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    // 取消滚动事件
    window.removeEventListener("scroll", this.scrollFn);
  },
  // 封装全部商品
  methods: {
    scrollFn() {
      console.log(
        "开始滚动",
        getScrollTop(),
        getClientHeight(),
        getScrollHeight()
      );
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 200) {
        if (this.goodsListShow.length >= this.goodsList.length) {
          // 没有数据之后的显示
          this.isTextbotBoom = true;
          return;
        }
        if (this.isloading == false) {
          this.isloading = true;
          console.log("可以输出新数据");
          //  从goodlist里面拿到数据 到goodlistShow数组中去
          this.page++; //加载下一页
          // console.log("1ds");
          console.log(this.page, this.size);
          // 模拟异步请求时长
          setTimeout(() => {
            for (
              //  每一页的页标减掉一个乘以每一页固定的数据八个就是下一个页标的起始点
              let i = (this.page - 1) * this.size;
              i < this.size * this.page;
              // 加载下一页
              i++
            ) {
              console.log("i", i, this.goodsList[i]);
              if (this.goodsList[i]) {
                this.goodsListShow.push(this.goodsList[i]);
              }
              // this.goodsList[i] && this.goodsListShow.push(this.goodsList[i]);
            }
            this.isloading = false; //加载完毕，结束
          }, 100);
        }
      }
    },
    async getGoods() {
      console.log(1);
      let res = await productsAPI({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      if (!res) return;
      console.log("res的值为:", res.data);
      this.goodsList = res.data;

      // 筛选八条数据出来,只显示八条数据
      this.goodsListShow = this.goodsList.filter((item, index) => index < 8);

      // 初始化判断当内容只有一页的时候的是否加载完毕
      this.isTextbotBoom = false;
      this.page = 1;
      // 判断新数据是否到达底部
      if (this.goodsListShow.length >= this.goodsList.length) {
        // 没有数据之后的显示
        this.isTextbotBoom = true;
        // return;
      }
    },
    options1Fn(index, min, max) {
      // console.log(1);
      this.options1num = index;
      this.min = min;
      this.max = max;
      this.getGoods();
    },
    options2Fn(index, type) {
      //设置所有的isactive都为false
      this.options2.forEach((item) => (item.isActive = false));
      //设置当前点击的为true
      this.options2[index].isActive = true;
      this.type = type;
      this.getGoods();
    },
  },
  watch: {
    // 解决页面搜索的问题
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal;
          this.getGoods();
        }
      },
    },
  },
};
</script>
 
<style lang = "less" scoped>
/* @import "../assets/public.less"; */

.goods {
  li {
    list-style: none;
  }
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      list-style: none;
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          list-style: none;
          /* color: @base-color;   */
          /* color: ; */
          font-weight: bold;
          /* color: red; */
        }
      }
    }
  }
}
</style>